package com.stx.House.CGLIB;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;

public class HouseBuyer {
    public void buyHouseByDynamicProxyForCglib() {
        //被代理的对象：房主
        HouseOwner owner = new HouseOwner();

        //CGLIB增强的代理类
        Enhancer enhancer = new Enhancer();

        //被代理对象的类型
        enhancer.setSuperclass(HouseOwner.class);

        //定义代理时的方法解释器
        MethodInterceptor methodInterceptor = new HouseAgentByDynamicForCglib(owner);

        //以回调方式设置代理行为
        enhancer.setCallback(methodInterceptor);

        //创建代理对象
        HouseOwner roomowner = (HouseOwner) enhancer.create();

        //代理售房
        roomowner.tradeHouse();
    }
}
